import 'package:flutter/material.dart';
import 'package:stage_app/api/api.dart';
import 'package:stage_app/constants/route_constant.dart';
import 'package:stage_app/main.dart';
import 'package:stage_app/model/user/user_login_result_entity.dart';
import 'package:stage_app/utils/http_utils.dart';
import 'package:stage_app/utils/sp_util.dart';
import 'package:stage_app/utils/util.dart';
import 'package:stage_app/wigets/button/round_button.dart';
import 'package:stage_app/wigets/login_item.dart';

class RegisterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false, // 可以通过设置 这个属性 防止键盘 覆盖内容 或者 键盘 撑起内容
      body: new Stack(
        children: <Widget>[
//          new Image.asset(Util.getImgPath('')),
          new LoginForm()
        ],
      ),
    );
  }
}

class LoginForm extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Text控件
    TextEditingController _ctlUsername = new TextEditingController();
    TextEditingController _ctlPassword = new TextEditingController();
    TextEditingController _ctlCaptcha = new TextEditingController();

    Future<void> _userLogin() async {
      String username = _ctlUsername.text;
      String password = _ctlPassword.text;
      String captcha = _ctlCaptcha.text;

      // TODO 正则校验
      if (username.isEmpty || username.length < 6) {
        Util.showSnackBar(context, username.isEmpty ? "请输入用户名～" : "用户名至少6位～");
        return;
      }
      if (password.isEmpty || password.length < 6) {
        Util.showSnackBar(context, username.isEmpty ? "请输入密码～" : "密码至少6位～");
        return;
      }

      var params = {
        'mobile': username,
        'password': password,
      };
      print('params => ' + params.toString());

      UserLoginResultEntity userLoginResultEntity =
          await HttpUtils.post<UserLoginResultEntity>(context, Api.REGISTER, params);
      sp.putString(SpKeyConstant.TOKEN_KEY, userLoginResultEntity.token);

      Util.showSnackBar(context, "注册成功～");

      Future.delayed(new Duration(milliseconds: 500), () {
        Navigator.of(context).pushReplacementNamed(RouteConstant.ROUTE_HOME);
      });
    }

    return new Column(
      children: <Widget>[
        new Container(
            margin: EdgeInsets.only(top: 60),
            child: new Text(
              "注册",
              style: new TextStyle(
                fontSize: 30,
              ),
            )),
        new Container(
            margin: EdgeInsets.only(left: 14, right: 14, top: 93),
            child: new Column(
//              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                LoginItem(
                  controller: _ctlUsername,
//                prefixIcon: Icons.person,
                  labelText: "用户名",
                  hintText: "请输入用户名",
                ),
//                  Gaps.vGap15,
                new Container(
                  margin: EdgeInsets.only(top: 30),
                  child: LoginItem(
                    controller: _ctlPassword,
//                prefixIcon: Icons.lock,
                    type: "password",
                    labelText: "密码",
                    hintText: "请输入密码",
                  ),
                ),
                new Container(
                  margin: EdgeInsets.only(top: 30),
                  child: LoginItem(
                    controller: _ctlPassword,
//                prefixIcon: Icons.lock,
                    type: "captcha",
                    maxLength: 6,
                    keyboardType: TextInputType.number,
                    labelText: "验证码",
                    hintText: "请输入验证码",
                  ),
                ),
                new Container(
                    margin: EdgeInsets.only(top: 39.5),
                    child: new RoundButton(
                      text: "注册",
                      margin: EdgeInsets.only(top: 20),
                      onPressed: () {
                        _userLogin();
                      },
                    )),
                new Container(
                    margin: EdgeInsets.only(top: 37.5),
                    child: new Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        new Text(
                          "已有账户？",
                        ),
                        new GestureDetector(
                          child: new Text(
                            '登录',
                            style: new TextStyle(color: Colors.amberAccent),
                          ),
                          onTap: () {
                            toPageLogin(context);
                          },
                        ),
                      ],
                    )),
              ],
            )),
      ],
    );
  }

  void toPageLogin(BuildContext context) {
    Navigator.of(context).pushNamedAndRemoveUntil(
        RouteConstant.ROUTE_LOGIN, (Route<dynamic> route) => false);
  }
}
